const path = require('path');
const { rewriteFileLine } = require('../../util/rewrite');
const { helMonoLog } = require('../../util');

module.exports = function replaceSubApp(/** @type {import('../../types').ICWDAppData} */ appData) {
  const { isSubMod, realAppPkgName, helDirPath } = appData;
  const subAppFilePath = isSubMod ? path.join(helDirPath, './configs/subApp.ts') : path.join(helDirPath, './subApp.ts');
  helMonoLog(`replace content of ${subAppFilePath}`);

  rewriteFileLine(subAppFilePath, (line) => {
    let targetLine = line;
    if (line.includes('[hel tip]')) {
      targetLine = ' * This file content is auto generated by hel script';
    } else if (line.includes('{{APP_PACK_NAME}}')) {
      targetLine = line.replace('{{APP_PACK_NAME}}', realAppPkgName);
    }
    return { line: targetLine };
  });
};
